package com.njzxw.fs.commonutil;

import com.njzxw.fs.commonutil.bean.SystemGroupBean;
import com.njzxw.fs.commonutil.repository.BaseRep;
import com.njzxw.fs.commonutil.util.LoadSpringBean;
import com.njzxw.fs.commonutil.web.BaseServer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 项目初始化启动的类
 */
@Service
public class Init extends BaseServer{

    @Autowired
    private BaseRep baseRep;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoadSpringBean loadSpringBean;

    @PostConstruct
    public void execute(){
        logger.debug("2###################初始化加载------------------start");
        //设置单一的选项

        updateDDW();


//        logger.debug("groupList:"+groupList);
        logger.debug("2###################初始化加载------------------end");
    }

    /**
     * 防止redis超时 设置10秒访问一次 模拟心跳链接
     */
    @Scheduled(cron = "0/50 * * * * *")
    public void timer() {
//        logger.debug("防止redis超时 设置10秒访问一次 模拟心跳链接=======");
        redisTemplate.opsForValue().get("heartbeat");
    }

    /**
     * 防止mysql超时 设置50秒访问一次 模拟心跳链接
     */
    @Scheduled(cron = "0/50 * * * * *")
    public void heatMysql() {
        baseRep.heatMysql();
    }

    /**
     * 获取配置信息 设置300秒更新一次
     */
    @Scheduled(cron = "0/300 * * * * *")
    public void updateDDW() {

        List<SystemGroupBean> groupList = baseRep.getSystemGroupSiglBean();
        groupList.addAll(baseRep.getSystemGroupMoreBean());

        for(int i=0;i<groupList.size();i++){
            String groupcode = groupList.get(i).getGroupcode();
            String values = groupList.get(i).getValues();
            if(StringUtils.isBlank(values)){
                groupList.get(i).setItemList(baseRep.getSystemGroupItemBean(groupcode));
            }
        }
        common.setSystemGroupList(groupList);
    }

}
